﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZX.BLL.Extend;
using ZX.SYS.Library;
using ZX.EHR.Library;
using ZX.Common;
using ZX.Common.Extend;
using System.Globalization;

//==================================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 作 者：it012
// Email: 
// 日 期：2012/3/28
// 描 述：员工基本信息业务处理
// 版 本：1.0.0.0    
//    
//==================================================================

namespace ZX.BLL
{
    public class BwkCalenderSolution : BaseBussnies
    {
        public override bool Insert(object entitys, out string msg)
        {
            bool rs = base.Insert(entitys, out msg);
            if (rs)//插入成功后自动生成日历节假日数据
            {
                wkCalenderSolution wkcs = entitys as wkCalenderSolution;
                this.CreateCalendarDetailData(wkcs);
            }
            msg = "test --" + msg;
            return rs;
        }

        public override bool Update(object entitys, out string msg)
        {
            bool rs= base.Update(entitys, out msg);
            wkCalenderSolution wkcs = entitys as wkCalenderSolution;
            if (rs)
            {
                try
                {
                    if (wkcs.isDefault == true)//默认的
                    {
                        //将中旭股份的日历改为当前
                        wkDeptCalenderSolution wkdeptcs = new wkDeptCalenderSolution();
                        BaseBussnies wkdeptbll = new BaseBussnies(wkdeptcs);
                        wkdeptcs = wkdeptbll.GetEntityByCondition("DeptID='333FE8ED-CA2A-497A-A88C-3BB4FB3A1A57'") as wkDeptCalenderSolution;
                        if (wkdeptcs == null) //当前没有默认日历
                        {
                            wkdeptcs = new wkDeptCalenderSolution();
                            wkdeptcs.ID = Guid.NewGuid();
                            wkdeptcs.DeptID = new Guid("333FE8ED-CA2A-497A-A88C-3BB4FB3A1A57");
                            wkdeptcs.CalenderSolutionID = wkcs.ID;
                            wkdeptcs.AddUser = wkcs.AddUser;
                            wkdeptcs.AddTime = wkcs.AddTime;
                            wkdeptbll.Insert(wkdeptcs);
                        }
                        else
                        {
                            wkdeptcs.CalenderSolutionID = wkcs.ID;//更新默认日历
                            wkdeptbll.Update(wkdeptcs);
                        }
                        //将其他日历默认属性去除
                        wkCalenderSolution wkcs2 = new wkCalenderSolution();
                        BaseBussnies wkcsbll2 = new BaseBussnies(wkcs2);
                        IList<DBModule.BaseEntity> list = wkcsbll2.GetEntitysByCondition("isDefault=1 and ID!='" + wkcs.ID + "'");
                        if (list == null || list.Count == 0) return rs;
                        for (int i = 0; i < list.Count; i++)
                        {
                            wkCalenderSolution tempwkcs = list[i] as wkCalenderSolution;
                            tempwkcs.isDefault = false;
                            wkcsbll2.Update(tempwkcs);
                        }
                    }
                }
                catch
                { }
            }
            return rs;
        }

        private void CreateCalendarDetailData(wkCalenderSolution wkcs)
        {
            try
            {
                //起如年份的1月1日已生成不需要再生成
                //阳历节假日
                for (int i = wkcs.BeginYear; i < wkcs.EndYear + 1; i++)
                {
                    DateTime cjDate = ChineseCalendarInfo.GetDateFromLunarDate(i, 1, 1, false);//春节
                    DateTime zqDate = ChineseCalendarInfo.GetDateFromLunarDate(i, 8, 15, false);//中秋
                    DateTime dwDate = ChineseCalendarInfo.GetDateFromLunarDate(i, 5, 5, false);//端午
                    string qm = new ChineseCalendarInfo().SolarTermQingMing(i);//获取清明日期
                    List<string> listData = new List<string>();
                    listData.Add(i + "-01-01");
                    listData.Add(i + "-05-01");
                    listData.Add(i + "-10-01");
                    listData.Add(i + "-10-02");
                    listData.Add(i + "-10-03");
                    listData.Add(cjDate.ToString("yyyy-MM-dd"));
                    listData.Add(cjDate.AddDays(1).ToString("yyyy-MM-dd"));
                    listData.Add(cjDate.AddDays(2).ToString("yyyy-MM-dd"));
                    if (!listData.Contains(zqDate.ToString("yyyy-MM-dd")))
                    {
                        listData.Add(zqDate.ToString("yyyy-MM-dd"));
                    }
                    listData.Add(dwDate.ToString("yyyy-MM-dd"));
                    listData.Add(qm);
                    foreach (string s in listData)
                    {
                        wkCalendar wkcalendar = new wkCalendar();
                        wkcalendar.ID = Guid.NewGuid();
                        wkcalendar.SolutionID = wkcs.ID;
                        wkcalendar.DayType = "1";
                        wkcalendar.Holiday = DateTime.Parse(s);
                        wkcalendar.AddUser = wkcs.AddUser;
                        wkcalendar.AddTime = wkcs.AddTime;
                        BaseBussnies bll = new BaseBussnies(wkcalendar);
                        bll.Insert();
                    }
                    //获取该年中所有周末
                    List<string> wenkendList = ChineseCalendarInfo.TheWenkEndOfYear(i);
                    foreach (string s in wenkendList)
                    {
                        if (!listData.Contains(s))
                        {
                            wkCalendar wkcalendar = new wkCalendar();
                            wkcalendar.ID = Guid.NewGuid();
                            wkcalendar.SolutionID = wkcs.ID;
                            wkcalendar.DayType = "0";
                            wkcalendar.Holiday = DateTime.Parse(s);
                            wkcalendar.AddUser = wkcs.AddUser;
                            wkcalendar.AddTime = wkcs.AddTime;
                            BaseBussnies bll = new BaseBussnies(wkcalendar);
                            bll.Insert();
                        }
                    }
                }
            }
            catch { }
            try
            {
                if (wkcs.isDefault == true)//默认的
                { 
                    //将中旭股份的日历改为当前
                    wkDeptCalenderSolution wkdeptcs = new wkDeptCalenderSolution();
                    BaseBussnies wkdeptbll = new BaseBussnies(wkdeptcs);
                    wkdeptcs = wkdeptbll.GetEntityByCondition("DeptID='333FE8ED-CA2A-497A-A88C-3BB4FB3A1A57'") as wkDeptCalenderSolution;
                    if (wkdeptcs == null) //当前没有默认日历
                    {
                        wkdeptcs = new wkDeptCalenderSolution();
                        wkdeptcs.ID = Guid.NewGuid();
                        wkdeptcs.DeptID = new Guid("333FE8ED-CA2A-497A-A88C-3BB4FB3A1A57");
                        wkdeptcs.CalenderSolutionID = wkcs.ID;
                        wkdeptcs.AddUser = wkcs.AddUser;
                        wkdeptcs.AddTime = wkcs.AddTime;
                        wkdeptbll.Insert(wkdeptcs);
                    }
                    else
                    {
                        wkdeptcs.CalenderSolutionID = wkcs.ID;//更新默认日历
                        wkdeptbll.Update(wkdeptcs);
                    }
                    //将其他日历默认属性去除
                    wkCalenderSolution wkcs2=new wkCalenderSolution();
                    BaseBussnies wkcsbll2 = new BaseBussnies(wkcs2);
                    IList<DBModule.BaseEntity> list = wkcsbll2.GetEntitysByCondition("isDefault=1 and ID!='" + wkcs.ID + "'");
                    if (list == null || list.Count == 0) return;
                    for (int i = 0; i < list.Count; i++)
                    {
                        wkCalenderSolution tempwkcs = list[i] as wkCalenderSolution;
                        tempwkcs.isDefault = false;
                        wkcsbll2.Update(tempwkcs);
                    }
                }
            }
            catch 
            { }
        }
    }
}
